<?php
/************************** Auto-creator for all Custom Templates **************************/
// routine for auto-create New Page and assign it to Custom Template :: By DAMEER, Sofarider
global $user_ID;

/* CHECKOUT PAGE */
$co_exist = TRUE;
$co_locale = __( "Checkout", "sofa_suppastore" );
if( intval( get_option( 'checkout' ) ) == 0 || !get_option( 'checkout' ) ) {
    add_option( 'checkout' ); // add dummy option
    $co_exist = FALSE;
} else {
    $page_obj = get_post( intval( get_option( 'checkout' ) ) ); // check whether there's a page corresponding to that ID
    if( !$page_obj ) $co_exist = FALSE;
}
if( !$co_exist ) {
    $new_page_co = array( 'post_title' => $co_locale, 'post_content' => '', 'post_status' => 'publish', 'post_date' => date('Y-m-d H:i:s'), 'post_author' => $user_ID, 'post_type' => 'page', 'post_category' => array(0) );
    $page_id = wp_insert_post( $new_page_co );
    if( $page_id ) {
        update_post_meta( $page_id, '_wp_page_template', 'checkout.php' );
        update_option( 'checkout', $page_id );
    }
}
/* ORDERS OVERVIEW PAGE */
$oo_exist = TRUE;
$oo_locale = __( "Orders Overview", "sofa_suppastore" );
if( intval( get_option( 'ordersoverview' ) ) == 0 || !get_option( 'ordersoverview' ) ) {
    add_option( 'ordersoverview' ); // add dummy option
    $oo_exist = FALSE;
} else {
    $page_obj_oo = get_post( intval( get_option( 'ordersoverview' ) ) ); // check whether there's a page corresponding to that ID
    if( !$page_obj_oo ) $oo_exist = FALSE;
}
if( !$oo_exist ) {
    $new_page_oo = array( 'post_title' => $oo_locale, 'post_content' => '', 'post_status' => 'publish', 'post_date' => date('Y-m-d H:i:s'), 'post_author' => $user_ID, 'post_type' => 'page', 'post_category' => array(0) );
    $page_id_oo = wp_insert_post( $new_page_oo );
    if( $page_id_oo ) {
        update_post_meta( $page_id_oo, '_wp_page_template', 'orders_overview.php' );
        update_option( 'ordersoverview', $page_id_oo );
    }
}
/* STOCK INVENTORY PAGE */
$si_exist = TRUE;
$si_locale = __( "Stock Inventory", "sofa_suppastore" );
if( intval( get_option( 'stockinventory' ) ) == 0 || !get_option( 'stockinventory' ) ) {
    add_option( 'stockinventory' ); // add dummy option
    $si_exist = FALSE;
} else {
    $page_obj_si = get_post( intval( get_option( 'stockinventory' ) ) ); // check whether there's a page corresponding to that ID
    if( !$page_obj_si ) $si_exist = FALSE;
}
if( !$si_exist ) {
    $new_page_si = array( 'post_title' => $si_locale, 'post_content' => '', 'post_status' => 'publish', 'post_date' => date('Y-m-d H:i:s'), 'post_author' => $user_ID, 'post_type' => 'page', 'post_category' => array(0) );
    $page_id_si = wp_insert_post( $new_page_si );
    if( $page_id_si ) {
        update_post_meta( $page_id_si, '_wp_page_template', 'stock_inventory.php' );
        update_option( 'stockinventory', $page_id_si );
    }
}
/* HOME PAGE LAYOUTS */
// full width with featured products arranged in grid
$hpw_full_exist = TRUE;
$hpw_full_locale = __( "Welcome Full Width", "sofa_suppastore" );
if( intval( get_option( 'welcomefull' ) ) == 0 || !get_option( 'welcomefull' ) ) {
    add_option( 'welcomefull' ); // add dummy option
    $hpw_full_exist = FALSE;
} else {
    $page_obj_hpw_full = get_post( intval( get_option( 'welcomefull' ) ) ); // check whether there's a page corresponding to that ID
    if( !$page_obj_hpw_full ) $hpw_full_exist = FALSE;
}
if( !$hpw_full_exist ) {
    $new_page_hpw_full = array( 'post_title' => $hpw_full_locale, 'post_content' => 'This is alternative Home page layout. Full width page with an intro text (you are reading it right now) and grid arranged featured products.', 'post_status' => 'publish', 'post_date' => date('Y-m-d H:i:s'), 'post_author' => $user_ID, 'post_type' => 'page', 'post_category' => array(0) );
    $page_id_hpw_full = wp_insert_post( $new_page_hpw_full );
    if( $page_id_hpw_full ) {
        update_post_meta( $page_id_hpw_full, '_wp_page_template', 'index_full_products.php' );
        update_option( 'welcomefull', $page_id_hpw_full );
    }
}
// content/narrow width with featured products presented in scroller
$hpw_narrow_grid_exist = TRUE;
$hpw_narrow_grid_locale = __( "Welcome with Sidebar, products in grid", "sofa_suppastore" );
if( intval( get_option( 'welcomenarrowgrid' ) ) == 0 || !get_option( 'welcomenarrowgrid' ) ) {
    add_option( 'welcomenarrowgrid' ); // add dummy option
    $hpw_narrow_grid_exist = FALSE;
} else {
    $page_obj_hpw_narrow_grid = get_post( intval( get_option( 'welcomenarrow' ) ) ); // check whether there's a page corresponding to that ID
    if( !$page_obj_hpw_narrow_grid ) $hpw_narrow_grid_exist = FALSE;
}
if( !$hpw_narrow_grid_exist ) {
    $new_page_hpw_narrow_grid = array( 'post_title' => $hpw_narrow_grid_locale, 'post_content' => 'This is alternative Home page layout. Normal/content wide page with an intro text (you are reading it right now) and all of featured products presented in jQuery scroller.', 'post_status' => 'publish', 'post_date' => date('Y-m-d H:i:s'), 'post_author' => $user_ID, 'post_type' => 'page', 'post_category' => array(0) );
    $page_id_hpw_narrow_grid = wp_insert_post( $new_page_hpw_narrow_grid );
    if( $page_id_hpw_narrow_grid ) {
        update_post_meta( $page_id_hpw_narrow_grid, '_wp_page_template', 'index_narrow_grided.php' );
        update_option( 'welcomenarrowgrid', $page_id_hpw_narrow_grid );
    }
}
// content/narrow width with featured products presented in scroller
$hpw_narrow_exist = TRUE;
$hpw_narrow_locale = __( "Welcome with Sidebar", "sofa_suppastore" );
if( intval( get_option( 'welcomenarrow' ) ) == 0 || !get_option( 'welcomenarrow' ) ) {
    add_option( 'welcomenarrow' ); // add dummy option
    $hpw_narrow_exist = FALSE;
} else {
    $page_obj_hpw_narrow = get_post( intval( get_option( 'welcomenarrow' ) ) ); // check whether there's a page corresponding to that ID
    if( !$page_obj_hpw_narrow ) $hpw_narrow_exist = FALSE;
}
if( !$hpw_narrow_exist ) {
    $new_page_hpw_narrow = array( 'post_title' => $hpw_narrow_locale, 'post_content' => 'This is alternative Home page layout. Normal/content wide page with an intro text (you are reading it right now) and all of featured products presented in jQuery scroller.', 'post_status' => 'publish', 'post_date' => date('Y-m-d H:i:s'), 'post_author' => $user_ID, 'post_type' => 'page', 'post_category' => array(0) );
    $page_id_hpw_narrow = wp_insert_post( $new_page_hpw_narrow );
    if( $page_id_hpw_narrow ) {
        update_post_meta( $page_id_hpw_narrow, '_wp_page_template', 'index_narrow_scrolled.php' );
        update_option( 'welcomenarrow', $page_id_hpw_narrow );
    }
}
// common blog (good old blog)
$hpw_common_exist = TRUE;
$hpw_common_locale = __( "Welcome to Blog", "sofa_suppastore" );
if( intval( get_option( 'commonblog' ) ) == 0 || !get_option( 'commonblog' ) ) {
    add_option( 'commonblog' ); // add dummy option
    $hpw_common_exist = FALSE;
} else {
    $page_obj_hpw_common = get_post( intval( get_option( 'commonblog' ) ) ); // check whether there's a page corresponding to that ID
    if( !$page_obj_hpw_common ) $hpw_common_exist = FALSE;
}
if( !$hpw_common_exist ) {
    $new_page_hpw_common = array( 'post_title' => $hpw_common_locale, 'post_content' => 'This is alternative Home page layout. Use it only in case you\'d like to have blog posts displayed on home page instead of products.', 'post_status' => 'publish', 'post_date' => date('Y-m-d H:i:s'), 'post_author' => $user_ID, 'post_type' => 'page', 'post_category' => array(0) );
    $page_id_hpw_common = wp_insert_post( $new_page_hpw_common );
    if( $page_id_hpw_common ) {
        update_post_meta( $page_id_hpw_common, '_wp_page_template', 'index_common_blog.php' );
        update_option( 'commonblog', $page_id_hpw_common );
    }
}